package com.lb.activity.rest;

import com.lb.activity.po.Permission;
import com.lb.activity.service.PermissionService;
import com.lb.activity.util.JsonUtil;
import com.lb.activity.util.PermissionTree;
import com.lb.activity.util.ResultUtil;
import com.lb.activity.vo.RoleVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author liubin
 */
@RestController
@Api(value = "权限管理")
@RequestMapping("permission")
@Slf4j
public class PermissionController {

    @Autowired
    private PermissionService permissionService;

    @GetMapping("toPermission")
    @ApiOperation(value = "跳转到permission页面")
    public ModelAndView toRole(){
        return new ModelAndView("permission/menus");
    }


    @GetMapping("list")
    @ApiOperation(value = "分页查询权限",notes = "分页查询")
    public String list(RoleVo roleVo){
        List<Permission> adminPermissions= permissionService.AllMeuns();
        String json="";
        try {
            json = JsonUtil.objectToJson(adminPermissions)+"";
        } catch (IOException e) {
            e.printStackTrace();
        }
        if(json.contains("id")){
            json=json.replaceAll("id","menuId").replaceAll("parentmenuId","parentId");
        }
        return json;
    }

    @GetMapping("icons")
    @ApiOperation(value = "icon页面")
    public ModelAndView fontIcoList(){
        return  new ModelAndView("permission/icons");
    }

    @PostMapping("add")
    @ApiOperation(value = "添加或者更新权限")
    public ModelAndView add(Permission permission){
        permissionService.add(permission);
        return new ModelAndView("redirect:/permission/toPermission");
    }

    @PostMapping("getOneByName")
    @ApiOperation(value = "获取菜单是否存在",notes = "传递名称和id")
    public ResultUtil getOneByName(String name,Long id){
        ResultUtil resultUtil = permissionService.getOneByName(name,id);
        return resultUtil;
    }

    @PostMapping("getOne")
    @ApiOperation(value = "根据id获取",notes = "传递id")
    public Permission getOne(Long id){
        Permission permission = permissionService.getOne(id);
        return permission;
    }


    @PostMapping("delete")
    @ApiOperation(value = "删除权限")
    public ResultUtil delete(Long id){
        ResultUtil resultUtil = permissionService.delete(id);
        return resultUtil;
    }

    @PostMapping("changeStatus")
    @ApiOperation(value = "修改权限的状态")
    public ResultUtil changeStatus(Long id,boolean boolStatus){
        ResultUtil resultUtil = permissionService.changeStatus(id,boolStatus);
        return resultUtil;
    }

    @PostMapping("menuTree")
    @ApiOperation(value = "获取权限树",notes = "根据角色的id获取权限详情")
    public PermissionTree<Permission> menuTree(Long roleId) {
        PermissionTree<Permission> tree = permissionService.getTree(roleId);
        return tree;
    }

    @PostMapping("modifyRoleMenus")
    @ApiOperation(value = "编辑或修改用户权限",notes = "传递数组参数")
    public Object modifyRoleMenus(Long id,String[] menuIds){
        try {
            List<String> menus=new ArrayList<>();
            if(menuIds!=null){
                menus = Arrays.asList(menuIds);
            }
            permissionService.updateRoleAndmenuIds(id,menus);
        } catch (Exception e) {
            log.info("/modifyRoleMenus 分配权限失败");
            return ResultUtil.build(400,"分配权限失败");
        }
        return ResultUtil.ok();
    }


}
